package com.huluobo.a2201mvvm_imdemo;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.huluobo.a2201mvvm_imdemo.adapter.PagerFragmentAdapter;
import com.huluobo.a2201mvvm_imdemo.addFriend.AddFriendActivity;
import com.huluobo.a2201mvvm_imdemo.addGroup.AddGroupActivity;
import com.huluobo.a2201mvvm_imdemo.contact.ContactFragment;
import com.huluobo.a2201mvvm_imdemo.contact.MyContactFragment;
import com.huluobo.a2201mvvm_imdemo.conversation.ConversationFragment;
import com.huluobo.a2201mvvm_imdemo.conversation.MyConversationFragment;
import com.huluobo.a2201mvvm_imdemo.databinding.ActivityMainBinding;
import com.huluobo.a2201mvvm_imdemo.mine.MineFragment;
import com.huluobo.a2201mvvm_imdemo.mvvm.BaseActivity;
import com.huluobo.a2201mvvm_imdemo.shop.ShopFragment;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private PagerFragmentAdapter adapter;

    private List<Fragment> fragments = new ArrayList<>();

    private View badge;
    private TextView count;

    @Override
    protected void initData() {
        fragments.clear();
//        fragments.add(new ConversationFragment());
        fragments.add(new MyConversationFragment());
//        fragments.add(new ContactFragment());
        fragments.add(new MyContactFragment());
        fragments.add(new ShopFragment());
        fragments.add(new MineFragment());

        adapter = new PagerFragmentAdapter(this, fragments);
        dataBinding.mainVp.setAdapter(adapter);
    }

    @Override
    protected void initView() {
        requestPermissions(new String[]{"android.permission.CAMERA", "android.permission.RECORD_AUDIO"}, 102);

        //vp2与底部导航联动
        dataBinding.mainVp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                switch (position) {
                    case 0:
                        dataBinding.mainNav.getMenu().getItem(0).setChecked(true);
                        dataBinding.mainHeader.headerTitle.setText("微信");
                        dataBinding.mainHeader.headerAdd.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        dataBinding.mainNav.getMenu().getItem(1).setChecked(true);
                        dataBinding.mainHeader.headerTitle.setText("联系人");
                        dataBinding.mainHeader.headerAdd.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        dataBinding.mainNav.getMenu().getItem(2).setChecked(true);
                        dataBinding.mainHeader.headerTitle.setText("商品");
                        dataBinding.mainHeader.headerAdd.setVisibility(View.GONE);
                        break;
                    case 3:
                        dataBinding.mainNav.getMenu().getItem(3).setChecked(true);
                        dataBinding.mainHeader.headerTitle.setText("我的");
                        dataBinding.mainHeader.headerAdd.setVisibility(View.GONE);
                        break;
                }
            }
        });

        //底部导航与vp2联动
        dataBinding.mainNav.setOnItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.tab_conversation:
                    dataBinding.mainVp.setCurrentItem(0);
                    dataBinding.mainHeader.headerTitle.setText("微信");
                    dataBinding.mainHeader.headerAdd.setVisibility(View.VISIBLE);
                    break;
                case R.id.tab_contact:
                    dataBinding.mainVp.setCurrentItem(1);
                    dataBinding.mainHeader.headerTitle.setText("联系人");
                    dataBinding.mainHeader.headerAdd.setVisibility(View.VISIBLE);
                    break;
                case R.id.tab_shop:
                    dataBinding.mainVp.setCurrentItem(2);
                    dataBinding.mainHeader.headerTitle.setText("商品");
                    dataBinding.mainHeader.headerAdd.setVisibility(View.GONE);
                    break;
                case R.id.tab_mine:
                    dataBinding.mainVp.setCurrentItem(3);
                    dataBinding.mainHeader.headerTitle.setText("我的");
                    dataBinding.mainHeader.headerAdd.setVisibility(View.GONE);
                    break;
            }
            return true;
        });
        //初始化时是第一个页面,所以需要将add按钮韩式出来
        dataBinding.mainHeader.headerAdd.setVisibility(View.VISIBLE);

        //add按钮添加创建菜单
        dataBinding.mainHeader.headerAdd.setOnClickListener(v -> {
            //实例化popupMenu
            //第一参数上下文,第二个参数 在那个view上弹出这个menu
            PopupMenu popupMenu = new PopupMenu(this, dataBinding.mainHeader.headerAdd);
            //引入menu的布局
            popupMenu.inflate(R.menu.add_menu);
            //添加menu的点击事件
            popupMenu.setOnMenuItemClickListener(item -> {
                switch (item.getItemId()) {
                    case R.id.addFriend:
                        Intent intentAddFriend = new Intent(this, AddFriendActivity.class);
                        startActivity(intentAddFriend);
                        break;
                    case R.id.addGroup:
                        Intent intentAddGroup = new Intent(this, AddGroupActivity.class);
                        startActivity(intentAddGroup);
                        break;
                    case R.id.scan:
                        scan();
                        break;
                    case R.id.pay:
                        ToastUtils.showLong("付款码");
                        break;
                }
                return true;
            });
            //一定别忘记让menu show出来
            popupMenu.show();
        });

        //角标香瓜
        //1.拿到menuView,也就是底部导航的menu
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) dataBinding.mainNav.getChildAt(0);
        //2.通过menu拿到item,也就是第一个item
        BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(0);

        //3.通过itemView去添加一个角标的布局
        badge = LayoutInflater.from(this).inflate(R.layout.view_badge, itemView, false);
        itemView.addView(badge);
        count = badge.findViewById(R.id.badge_tv);
        count.setText(EMClient.getInstance().chatManager().getUnreadMessageCount() + "");

        //添加消息监听器来监听接受消息时更新角标
        EMClient.getInstance().chatManager().addMessageListener(messages -> {
            runOnUiThread(() -> {
                badge.setVisibility(View.VISIBLE);
                count.setText(String.valueOf(EMClient.getInstance().chatManager().getUnreadMessageCount()));
            });
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (EMClient.getInstance().chatManager().getUnreadMessageCount() == 0) {
            badge.setVisibility(View.GONE);
        } else {
            badge.setVisibility(View.VISIBLE);
            count.setText(String.valueOf(EMClient.getInstance().chatManager().getUnreadMessageCount()));
        }
    }

    //扫一扫添加好友
    private void scan() {
        Intent intent = new Intent(this, CaptureActivity.class);
        startActivityForResult(intent, 101);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == RESULT_OK) {
            String result = data.getExtras().get(CodeUtils.RESULT_STRING).toString();
            EMClient.getInstance().contactManager().aysncAddContact(result, "", new EMCallBack() {
                @Override
                public void onSuccess() {
                    runOnUiThread(() -> ToastUtils.showLong("扫一扫添加好友成功"));
                }

                @Override
                public void onError(int code, String error) {
                    runOnUiThread(() -> ToastUtils.showLong("扫一扫添加好友失败" + error + ",code:" + code));
                }
            });
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
}